import { Component, Input, OnChanges } from '@angular/core';
import * as moment from 'moment';
import { Router } from '@angular/router';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';

import { TaskService } from '../../_common/_service/task.service';
import { ListService } from '../../_common/_service/list.service';
import { UpdateTaskDto } from '../../_common/_dtos/updateTaskDto';
import { Task } from '../../_common/_model/task';
import { List } from '../../_common/_model/list';
import { Functions } from '../../_common/functions';

@Component({
  selector: 'app-todo-contextmenu',
  templateUrl: './contextmenu.component.html',
  styleUrls: ['./contextmenu.component.less']
})
export class TodoContextmenuComponent implements OnChanges {
  @Input() currentTask: Task;
  @Input() visible: boolean;
  @Input() x: number;
  @Input() y: number;
  lists: List[];

  constructor(
    private _router: Router,
    private _taskService: TaskService,
    private _listService: ListService,
    private _funcs: Functions,
    private _message: NzMessageService) { }

  ngOnChanges() {
    this.lists = this._listService.lists;
  }

  get locationCSS() {
    return {
      'display': this.visible ? 'block' : 'none',
      'left': this.x + 'px',
      'top': this.y + 'px'
    };
  }

  isDefaultIcon(icon): boolean {
    return this._funcs.isDefaultIcon(icon);
  }

  toggleToday() {
    const { listId, index, today } = this.currentTask;
    const dto: UpdateTaskDto = {
      listId,
      index,
      ['today']: !today
    };
    this._taskService.updateTask(dto)
      .subscribe(
      next => {
        this._listService.getTasksByListId(listId)
          .find(u => u.index === index)
          .today = !today;
      },
      err => {
        this._message.error(err.msg);
      });
  }

  toggleDone() {
    const { listId, index, isDone } = this.currentTask;
    const dto = {
      listId,
      index,
      ['isDone']: !isDone
    };
    this._taskService.updateTask(dto).subscribe(next => {
      const currentList = this._listService.lists.find(u => u.id === listId);
      dto.isDone ? currentList.number -- : currentList.number ++;
      currentList.tasks.find(u => u.index === index).isDone = dto.isDone;
    });
  }

  toBeDoneToday() {
    console.log('to be done today', this.currentTask);
  }

  toBeDoneTomorrow() {
    const { listId, index } = this.currentTask;
    const dto: UpdateTaskDto = {
      listId,
      index,
      ['deadline']: moment().add(1, 'd').toDate()
    };
    this._taskService.updateTask(dto)
      .subscribe(
      next => {
        this._listService.getTasksByListId(listId)
          .find(u => u.index === index)
          .deadline = (dto['deadline'] as Date).toJSON();
      },
      err => {
        this._message.error(err.msg);
      }
      );
  }

  deleteDeadline() {
    const { listId, index } = this.currentTask;
    this._taskService.clearDeadline(listId, index)
      .subscribe(
      next => {
        this._listService.getTasksByListId(listId)
          .find(u => u.index === index)
          .deadline = null;
      },
      err => {
        this._message.error(err.msg);
      }
      );
  }

  moveTo(toListId: string) {
    // 向目标清单添加当前待办事项
    const toList = this.lists.find(u => u.id === toListId);
    const toTask: Task = {
      ...this.currentTask, index: toList.tasks.length, listId: toListId
    };
    this._taskService.moveToAnotherList(toTask)
      .subscribe(
      next => toList.tasks.push(toTask),
      err => this._message.error(err.msg)
      );
    // 把当前待办事项从所属清单中删除
    const { listId: ofListId, index } = this.currentTask;
    this._taskService.deleteTask(ofListId, index)
      .subscribe(
      next => {
        const ofList = this.lists.find(u => u.id === ofListId);
        const i = ofList.tasks.findIndex(u => u.index === index);
        ofList.tasks.splice(i, 1);
        ofList.tasks.forEach(u => {
          if (u.index > index) {
            u.index--;
          }
        });
      },
      err => {
        this._message.error(err.msg);
      });
  }

  deleteTask() {
    const { listId, index } = this.currentTask;
    this._taskService.deleteTask(listId, index)
      .subscribe(
      next => {
        const tasks = this._listService.getTasksByListId(listId);
        const _index = tasks.findIndex(u => u.index === index);
        tasks.splice(_index, 1);
        tasks.map(item => {
          if (item.index > index) {
            item.index--;
          }
        });
        const currentList = this._listService.lists.find(u => u.id === listId);
        if (currentList.number > 0) {
          currentList.number--;
        }
      }
      );
  }


}
